package org.example.test.controller.jquery;

import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.example.test.model.User;
import org.example.test.model.UserDetails;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "jquery-test", "/jquery-test", "/jquery-test/" })
public class JsonTestController {

	private final Logger LOGGER = LogManager.getLogger(getClass());
	private List<User> users;

	@RequestMapping(method = RequestMethod.GET)
	public String init() {
		LOGGER.info("Init users");
		users = testData();
		return "jquery-test";
	}

	@RequestMapping(value = "/all", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<User> getAllUsersAjax() {
		LOGGER.info("Get all users");
		return users;
	}

	private List<User> testData() {

		List<User> result = new ArrayList<User>();

		User user1 = new User.Builder("user1", "xxx").
				firstname("nikola").
				lastname("zaric").
				userDetails(new UserDetails.Builder().
						address("gagarinova").
						homeNumber(6).
						build()).
				build();

		User user2 = new User.Builder("user2", "xxx").
				firstname("neko ime").
				lastname("neko prezime").
				userDetails(new UserDetails.Builder().
						address("neka adresa").
						homeNumber(2).
						build()).
				build();

		result.add(user1);
		result.add(user2);

		return result;
	}

}
